
// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "VanTheLong_SAI_GLTest.h"
#include "ChildView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CChildView

CChildView::CChildView():m_BackBufferDC(NULL),m_BackBufferBitmap(NULL),m_Basket(NULL)
{
	m_bPause = false;
	m_bRuning = false;
	m_Tick=0;
	m_iLife = LIFE_BEGIN;
	m_iScore = 0;
	m_BackBufferBitmap = new CBitmap();
	m_BackBufferBitmap->LoadBitmap(IDB_BACKGROUND);
}

CChildView::~CChildView()
{

}


BEGIN_MESSAGE_MAP(CChildView, CWnd)
	ON_WM_PAINT()
	ON_WM_TIMER()
	ON_WM_KEYDOWN()
	ON_WM_ERASEBKGND()
	ON_WM_CREATE()
	ON_WM_CLOSE()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_OPTIONS_STOP32777, &CChildView::OnOptionsStop)
	ON_COMMAND(ID_OPTIONS_NEWGAME32775, &CChildView::OnOptionsNewgame)
	ON_COMMAND(ID_OPTIONS_P, &CChildView::OnOptionsPause_Play)
END_MESSAGE_MAP()



// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), NULL);

	return TRUE;
}

void CChildView::OnPaint() // ve mang hinh ban dau
{
	CPaintDC dc(this); // device context for painting
	CRect rect; 
	GetClientRect (&rect); 
	CFont font; 
	font.CreatePointFont (GM_TEXT_SIZE * 3, _T ("Arial"));
	dc.SelectObject (&font); 
	dc.SetBkMode (TRANSPARENT); 
	// Ve mang hinh ban dau...

	CString string = _T ("Welcome \n EGG game!!!"); 
	rect.OffsetRect (5, 5); 
	dc.SetTextColor (RGB (192, 192, 192)); 
	dc.DrawText (string, &rect, DT_CENTER ); 
	rect.OffsetRect (-2, -2); 
	dc.SetTextColor (RGB (0, 0, 0)); 
	dc.DrawText (string, &rect, DT_CENTER);

	string = _T ("PLAY"); 
	rect.OffsetRect (5, 5); 
	dc.SetTextColor (RGB (192, 192, 192)); 
	dc.DrawText (string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); 
	rect.OffsetRect (-2, -2); 
	dc.SetTextColor (RGB (0, 0, 0)); 
	dc.DrawText (string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);


}

void CChildView::OnTimer(UINT_PTR nIDEvent)
{
	if(m_bRuning == true)
	{
		static CClientDC dc(this);
		Game_OnPaint(&dc);
		if(m_bPause == false)
		{
			if(m_iLife == 0)
			{
				Game_Over();
			}
			m_Tick += TIME_UPDATE;
			m_Time_NewEgg_Current -= TIME_UPDATE;
			Egg_proc(m_Basket->Get_x());
			Animal_proc();
		}
	}
	//PlaySound(_T("D:\\badmove.wav"),NULL, SND_FILENAME | SND_SYNC);
	//PlaySound(_T("GameOver.WAV"),NULL,SND_FILENAME|SND_ASYNC);
	CWnd::OnTimer(nIDEvent);
	//dc.DeleteDC();
}


void CChildView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default
	if(nChar == VK_LEFT)
	{
		if(m_bPause==false)
		{
			m_Basket->Basket_Move(-BASKET_OFFSET_MOVE);
		}
	}

	if(nChar == VK_RIGHT)
	{
		if(m_bPause==false)
		{
			m_Basket->Basket_Move(BASKET_OFFSET_MOVE);
		}
	}
	if(nChar == 'N')
	{
		if(l_Animal.GetCount()<ANIMAL_MAX)
		{
			Animal_Add(_MASK);
		}
	}
	if(nChar == 'V')
	{
		if(l_Animal.GetCount()<ANIMAL_MAX)
		{
			Animal_Add(_DUCK);
		}
	}
	if(nChar == 'G')
	{
		if(l_Animal.GetCount()<ANIMAL_MAX)
		{
			Animal_Add(_CHICKEN);
		}
	}
	if(nChar == 'P')
	{
		m_bPause = !m_bPause;
	}
	CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}


BOOL CChildView::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default

	return CWnd::OnEraseBkgnd(pDC);
}



int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	//NewGame();
	return 0;
}


void CChildView::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	KillTimer(this->m_nTimer);	
	CWnd::OnClose();
}

void CChildView::Pause_Play()
{
	m_bPause = !m_bPause;
}
void CChildView::Stop()
{
	m_bRuning = false;
}
void CChildView::NewGame()
{
	if(m_bRuning == true)
	{
		return;
	}
	
	SetTimer(this->m_nTimer,TIME_UPDATE,NULL);
	if(m_Basket ==NULL)
	{
		m_Basket = new CBasket();
	}
	l_Animal.RemoveAll();
	l_Egg.RemoveAll();
	l_Animal.AddTail(CAnimal(100,_CHICKEN));
	m_iLevel = 1;
	m_iScore = 0;
	m_iLife = 3;
	m_Tick = 0;
	m_iEgg_Catched = 0;
	m_bPause = false;
	m_bRuning = true;
	m_iLevelup = 0;
	m_Time_NewEgg = EGG_NEW_LV1;
	m_Time_NewEgg_Current = m_Time_NewEgg;
	m_iEgg_Offset_Move = EGG_OFFSET_MOVE_LV1;
}

void CChildView::Animal_proc()
{
	POSITION pos;
	 //xu ly con thu
	if(m_Time_NewEgg_Current <= 0)
	{
		//de trung
		int m_Animal_Count = l_Animal.GetCount();
		int m_Animal_NewEgg = rand() %m_Animal_Count;
		pos = l_Animal.GetHeadPosition();
		int temp = 0;
		while(pos != NULL)
		{
			if(temp == m_Animal_NewEgg) // vi tri con thu de trung
			{
				int m_Type = rand()%5;
				if(m_Type == 2)//trung dat biet / shit
				{
					l_Egg.AddTail(CEgg(l_Animal.GetAt(pos).m_Cx+EGG_DC_X,m_iEgg_Offset_Move,EGG_SHIT));
				}
				else
				{
					l_Egg.AddTail(CEgg(l_Animal.GetAt(pos).m_Cx+EGG_DC_X,m_iEgg_Offset_Move,EGG_T1));
				}
			}
			l_Animal.GetNext(pos);
			temp++;
		}
		m_Time_NewEgg_Current = m_Time_NewEgg;
	}
	pos = l_Animal.GetHeadPosition();
	while(pos != NULL)
	{
		int _Dead = l_Animal.GetAt(pos).Animal_Proc();
		if(_Dead==1)
		{
			POSITION pos1 = pos;
			Animal_Remove(pos1);
		}
		l_Animal.GetNext(pos);
	}
}

void CChildView::Animal_Add(int m_iAnimal_Type)
{
	
	// them con thu
	if(m_bPause == false)
	{
		l_Animal.AddTail(CAnimal(50,m_iAnimal_Type));

		//thay doi toc do de trung
		switch (l_Animal.GetCount())
		{
		case 1:
			m_Time_NewEgg = EGG_NEW_LV1;
			break;
		case 2:
			m_Time_NewEgg = EGG_NEW_LV2;
			break;
		case 3:
			m_Time_NewEgg = EGG_NEW_LV3;
			break;
		case 4:
			m_Time_NewEgg = EGG_NEW_LV4;
			break;
		default:
			m_Time_NewEgg = EGG_NEW_LV5;
		}
	}
}
void CChildView::Animal_Remove(POSITION pos)
{
	l_Animal.RemoveAt(pos);
	//thay doi toc do de trung
	switch (l_Animal.GetCount())
	{

	case 0: case 1:
		m_Time_NewEgg = EGG_NEW_LV1;
		break;
	case 2:
		m_Time_NewEgg = EGG_NEW_LV2;
		break;
	case 3:
		m_Time_NewEgg = EGG_NEW_LV3;
		break;
	case 4:
		m_Time_NewEgg = EGG_NEW_LV4;
		break;
	default:
		m_Time_NewEgg = EGG_NEW_LV5;
	}
}

void CChildView::Egg_proc(int m_Cx)
{
	POSITION pos;
	POSITION pos_temp;
	pos = l_Egg.GetHeadPosition();
	while(pos != NULL)
	{
		pos_temp = pos;
		int temp = l_Egg.GetNext(pos).Egg_Proc(m_Basket->Get_x());
		if(temp != 0)
		{
			//PlaySound(SOUND_EEG_INIT);
			switch (temp)
			{
			case -2://Khong hung phai shit
				break;
			case -1://hung phai shit // khong hung duoc trung co ban
				m_iLife -= 1;
				break;
			case EGG_SCORE_T1: // hung duoc trung loai 1
				m_iScore += EGG_SCORE_T1;
				m_iEgg_Catched +=1;
				if(m_iEgg_Catched == GM_EGG_LEVEL)
				{
					Game_LevelUp();
				}
				break;
			default:
				break;
			}
			l_Egg.RemoveAt(pos_temp);
		}
	}
}


void CChildView::Game_Over()
{
	//Xu ly khi ket thuc game
	//PlaySound(_T("Sounds/GameOver.WAV"),NULL,SND_FILENAME|SND_SYNC);
	m_bRuning = false;
	m_bPause = true;
	//m_Basket = NULL;
	CString cs_Mess;
	cs_Mess.Format(TEXT("Your Score : %d\n\nDo you want a New Game"),m_iScore);
	MessageBox(cs_Mess,_T("Game Over"), MB_OK|MB_ICONASTERISK);
	//NewGame();
}

void CChildView::Game_LevelUp()
{
	//Xu ly khi nhan level up
	m_iEgg_Catched =0;
	m_iLevel += 1;
	m_iLevelup = 50;
	l_Egg.RemoveAll();
	switch (m_iLevel)
	{
	case 1:
		m_iEgg_Offset_Move = EGG_OFFSET_MOVE_LV1;
		break;
	case 2:
		m_iEgg_Offset_Move = EGG_OFFSET_MOVE_LV2;
		break;
	case 3:
		m_iEgg_Offset_Move = EGG_OFFSET_MOVE_LV3;
		break;
	case 4:
		m_iEgg_Offset_Move = EGG_OFFSET_MOVE_LV4;
		break;
	case 5:
		m_iEgg_Offset_Move = EGG_OFFSET_MOVE_LV5;
		break;
	default:
		m_iEgg_Offset_Move = EGG_OFFSET_MOVE_LVMAX;
		break;
	}
}

void CChildView::Game_OnPaint(CClientDC *dc)
{
	if (m_BackBufferDC==NULL)
	{
		m_BackBufferDC = new CDC;
		m_BackBufferDC->CreateCompatibleDC(dc);	
	}
	m_BackBufferDC->SelectObject(m_BackBufferBitmap);
	m_BackBufferDC->BitBlt(0,0,WD_WIDTH,WD_HEIGHT,dc,0,0,WHITENESS);

	CRect rect; 
	GetClientRect (&rect); 
	CFont font; 
	font.CreatePointFont (GM_TEXT_SIZE, _T ("Arial"));
	m_BackBufferDC->SelectObject (&font); 
	m_BackBufferDC->SetBkMode (TRANSPARENT); 

	CString string; 
	rect.OffsetRect (16, 16); 
	m_BackBufferDC->SetTextColor (RGB (192, 192, 192)); 

	string.Format(_T("Level : %d   Score : %d   Time : %d:%d"),m_iLevel,m_iScore,((m_Tick/1000)/60)%(60),(m_Tick/1000)%60);
	//m_BackBufferDC->TextOut (WD_WIDTH/3 - 5, GM_TEXT_Y, string);
	m_BackBufferDC->DrawText(string,rect,DT_CENTER|DT_SINGLELINE);

	// bottom
	string.Format(_T("Life : %d"),m_iLife);
	m_BackBufferDC->TextOut (WD_WIDTH/3, GM_BOTTOM + 5, string);
	//m_BackBufferDC->DrawText(string,rect,DT_CENTER|DT_SINGLELINE);

	if(m_bPause == true)
	{
		string = _T ("PAUSE"); 
		rect.OffsetRect (-5, -5); 
		m_BackBufferDC->SetTextColor (RGB (192, 192, 192)); 
		m_BackBufferDC->DrawText (string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); 
		rect.OffsetRect (-2, -2); 
		m_BackBufferDC->SetTextColor (RGB (0, 0, 0)); 
		m_BackBufferDC->DrawText (string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
	}

	if(m_iLevelup > 0)
	{
		string = _T ("LEVEL UP"); 
		rect.OffsetRect (-5, -5); 
		m_BackBufferDC->SetTextColor (RGB (192, 192, 192)); 
		m_BackBufferDC->DrawText (string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); 
		rect.OffsetRect (-2, -2); 
		m_BackBufferDC->SetTextColor (RGB (0, 0, 0)); 
		m_BackBufferDC->DrawText (string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
		m_iLevelup -=1;
	}
	
	//Line tren
	m_BackBufferDC->MoveTo(GM_LEFT,GM_TOP);
	m_BackBufferDC->LineTo(GM_RIGHT,GM_TOP);
	//Line giua
	m_BackBufferDC->MoveTo(GM_LEFT,GM_TOP + ANIMAL_HEIGHT + 10);
	m_BackBufferDC->LineTo(GM_RIGHT,GM_TOP + ANIMAL_HEIGHT + 10);
	//Line duoi
	m_BackBufferDC->MoveTo(GM_LEFT,GM_BOTTOM -5);
	m_BackBufferDC->LineTo(GM_RIGHT,GM_BOTTOM -5);

	//Ve lai gio
	m_Basket->Draw(m_BackBufferDC);
	// ve lai con thu
	POSITION pos;
	pos = l_Animal.GetHeadPosition();
	while(pos != NULL)
	{
		l_Animal.GetNext(pos).Draw(m_BackBufferDC);
	}
	// Ve lai so trung hien tai
	pos = l_Egg.GetHeadPosition();
	while(pos != NULL)
	{
		l_Egg.GetNext(pos).Draw(m_BackBufferDC);
	}
	dc->BitBlt(0,0,WD_WIDTH,WD_HEIGHT,m_BackBufferDC,0,0,SRCCOPY);
	//ReleaseDC(dc);
}



void CChildView::Dump(CDumpContext& dc) const
{
	CWnd::Dump(dc);

	// TODO: Add your specialized code here and/or call the base class
}


void CChildView::AssertValid() const
{
	CWnd::AssertValid();

	// TODO: Add your specialized code here and/or call the base class
}

void CChildView::PlayDatFunk(CString TheFile,CString TheCommand)
{
	CString TheDirectory = _T("Sounds/");
	if(TheCommand =="play")
	{
		CString LOAD_IT =_T("open\"" + TheDirectory + TheFile + "\"type mpegvideo alias" + TheFile);
		mciSendString(LOAD_IT,NULL,0,0);
		CString PLAY_IT = _T("play"+TheFile+"from 0");
		mciSendString(PLAY_IT,NULL,0,0);
	}
}


void CChildView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(m_bRuning == true)
	{
		m_bPause = !m_bPause;
	}
	else
	{
		NewGame();
	}
	CWnd::OnLButtonDblClk(nFlags, point);
}


void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(m_bRuning == true & m_bPause == false)
	{
		if((point.x > m_Basket->Get_x()) & (point.x < (m_Basket->Get_x() + BASKET_WIDTH))
			&(point.y > BASKET_Y)&(point.y < BASKET_Y+BASKET_HEIGHT))
		{
			//m_bPause = !m_bPause;
			m_bMouse = true;
			m_Mouse_x;
		}
	}
	CWnd::OnLButtonDown(nFlags, point);
}


void CChildView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	m_bMouse = false;
	CWnd::OnLButtonUp(nFlags, point);
}


void CChildView::OnMouseMove(UINT nFlags, CPoint point)
{
	//luu lai toa do x cua chuot
	// TODO: Add your message handler code here and/or call default
	if(m_bMouse == 1)
	{
		if((nFlags & MK_LBUTTON) == MK_LBUTTON)
		{
			m_Basket->Basket_Move(point.x-m_Mouse_x);
			m_Mouse_x = point.x;
		}
	}
	CWnd::OnMouseMove(nFlags, point);
}

void CChildView::Sound_BG()
{
	PlaySound(_T("D:\\badmove.wav"),NULL, SND_FILENAME | SND_SYNC);
}



void CChildView::OnOptionsStop()
{
	// TODO: Add your command handler code here
	Stop();
}


void CChildView::OnOptionsNewgame()
{
	// TODO: Add your command handler code here
	NewGame();
}


void CChildView::OnOptionsPause_Play()
{
	// TODO: Add your command handler code here
	Pause_Play();
}
